package com.accp.action;

import com.accp.biz.DoctorBiz;
import com.accp.pojo.Doctor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户业务控制器  usersAction负责调用模型业务类来实现用户的各种业务请求。
 * 接收客户端上传的参数方式：实现ModelDriven接口
 * @author Administrator
 */
public class DoctorAction implements ModelDriven<Doctor>{
	private Doctor doctor = new Doctor();//接收参数

	@Override
	public Doctor getModel() {
		return doctor;
	}
	
	private DoctorBiz doctorBiz;
	public void setDoctorBiz(DoctorBiz doctorBiz) {
		this.doctorBiz = doctorBiz;
	}

	/**
	 * 跳转修改医生页面
	 * @return
	 */
	public String toupdate(){
		Map<String,Object> req = (Map<String, Object>) ActionContext.getContext().get("request");
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Doctor doctor = doctorBiz.findByDoctorId(id);
		req.put("doctor", doctor);
		return "toupdate";
		}


	/**
	 * 修改用户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String update(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Doctor doctors = new Doctor();
		doctors.setId(doctor.getId());
		doctors.setName(doctor.getName());
		doctors.setEmail(doctor.getEmail());
		doctors.setPhone(doctor.getPhone());
		doctors.setSex(doctor.getSex());

		boolean flag = doctorBiz.update(doctors);
		if(flag){
			request.put("message", "修改成功");
			return "update";
		}
		return "error";
	}
	/**
	 * 添加用户
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String add(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		boolean flag = doctorBiz.add(doctor);
		if(flag){
			request.put("message", "添加成功");
			return "add";
		}
		return "error";
	}
	/**
	 * 删除用户
	 * @return
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Doctor doctor = doctorBiz.findByDoctorId(id);
		if(doctorBiz.deleteDoctor(doctor)){
			return "delete";
		}else{
			return "error";
		}
	}
	/**
	 * 分页查看用户信息
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String selectFirstPage(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		try{
			List list = doctorBiz.selectByPage("Doctor", 1, 3,doctor);
			int totalPage = doctorBiz.searchRecordsNextPage(3, "Doctor",doctor);
			request.put("list", list);
			request.put("totalPage", totalPage);
			request.put("currentPage", 1);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectFirstPage";
	}
	/**
	 * 分页查找下一页
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String selectNextPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int next = Integer.parseInt(request.getParameter("page"));
		try{
			Map<String,Object> session =  ActionContext.getContext().getSession();
			List list = doctorBiz.selectByPage("Doctor", next, 3,doctor);
			int total = doctorBiz.searchRecordsNextPage(3, "Doctor",doctor);
			request.setAttribute("list", list);
			request.setAttribute("currentPage", next);
			request.setAttribute("totalPage", total);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectNextPage";
	}
}
